package 多线程.Threadpool;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;

/**
 * runAsync()方法用Runnable函数时接口类型作为参数，无返回值，supplyAsync()方法以Supplier函数式接口作为参数，
 * 有返回值【用get()方法以阻塞的形式获取返回计算结果】。
 *
 * Executor参数可传可不传，若不传，就用默认的ForkJoinPool线程池，反之用传入的线程池。
 */
public class FourStaticDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.无返回值且默认线程池
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("无返回值");
        });

        //2.有返回值且自定义线程池
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("有返回值");
            return "hello world";
        }, Executors.newFixedThreadPool(1));
        //获取返回值（阻塞）
        String s = future1.get();
        System.out.println("s = " + s);
    }
}